package com.sdhs.analyze.service;

import com.sdhs.analyze.bean.InfoNodeStatusTrace;
import com.sdhs.analyze.bean.Threshold;
import com.sdhs.analyze.dao.InfoNodeStatusTraceMapper;
import com.sdhs.analyze.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class InfoNodeStatusTraceService {
    @Autowired
    private InfoNodeStatusTraceMapper infoNodeStatusTraceMapper;

    @Autowired
    private RedisUtil redisUtil;


    private String prefix = "InfoNodeStatusTrace";

    /**
     * 阈值缓存
     *
     * @param id
     * @return
     */
    @Cacheable(value = "threshold", key = "#id")
    public Threshold selectThreshold(Integer id) {
        return infoNodeStatusTraceMapper.selectThreshold(id);
    }

    /**
     * 异常记录缓存
     *
     * @param nodeId
     * @param nodeType
     * @return 放到redis 里面
     */
    public InfoNodeStatusTrace selectOne(String nodeId, String nodeType) {
/*        String key = prefix+":"+nodeId+":"+nodeType;
        String jsonStr = redisUtil.get(key);
        if(StringUtils.isNotBlank(jsonStr)){
            return JSONObject.parseObject(jsonStr,InfoNodeStatusTrace.class);
        }else{
            InfoNodeStatusTrace infoNodeStatusTrace = infoNodeStatusTraceMapper.selectOne(nodeId, nodeType);
            redisUtil.setEx(key,JSONObject.toJSONString(infoNodeStatusTrace),10, TimeUnit.MINUTES);
            return infoNodeStatusTrace;
        }*/
        return infoNodeStatusTraceMapper.selectOne(nodeId, nodeType);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(InfoNodeStatusTrace record) {
        infoNodeStatusTraceMapper.insert(record);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertHistory(InfoNodeStatusTrace record) {
        infoNodeStatusTraceMapper.insertHistory(record);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String nodeId, String nodeType) {
        infoNodeStatusTraceMapper.delete(nodeId, nodeType);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(InfoNodeStatusTrace record) {
        infoNodeStatusTraceMapper.update(record);
    }
}
